#!/bin/sh

rootdir="$1"

# 执行命令的辅助函数
run_in_chroot() {
    systemd-nspawn -D "$rootdir" bash -c "$@"
}

# 设置语言
run_in_chroot "
sed -i -E 's/#[[:space:]]?(en_US.UTF-8[[:space:]]+UTF-8)/\1/g' /etc/locale.gen
sed -i -E 's/#[[:space:]]?(zh_CN.UTF-8[[:space:]]+UTF-8)/\1/g' /etc/locale.gen

locale-gen
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales
"

# 微软提供的 wsl 启动器会调用adduser,需要将 USERS_GID 和 USERS_GROUP 注释。
run_in_chroot "sed -i -e 's/USERS_GID=100/#USERS_GID=100/' -e 's/USERS_GROUP=users/#USERS_GROUP=users/' /etc/adduser.conf"

# 清理缓存
run_in_chroot "
apt clean
rm -rf /var/cache/apt/archives/*
"